package com.springboot.spring03tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Component

public class AccountDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    //根据用户名更新余额
    @Transactional(propagation=Propagation.REQUIRED)//结账炸了，返回金额
    public void updateBalanceByUsername(String username, BigDecimal delta){
        //1、编写SQL
        String sql = "update account set balance = balance - ? where username = ?";
        //2..执行SQL
        jdbcTemplate.update(sql,delta,username);
    }


}
